package com.ys.other;

import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;

import java.io.*;
import java.net.URL;

/**
 * 描述: 使用多线程从网络下载一个资源到本地磁盘，路径可以自己灵活定义
 *
 * @author CodeYang
 * @version 2021/1/14 14:58
 */
public class P3Test {

    private static final String[] URL_PATH = {
            "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2568880985,498693560&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3279854203,3448694990&fm=26&gp=0.jpg",
            "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=19860963,3341724388&fm=26&gp=0.jpg",
            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1127508905,2240631993&fm=26&gp=0.jpg"};


    public static void main(String[] args) {
        // 1: 下载网络资源,URL,分析得到数据类型与文件后缀
        // 1: 多线程遍历下载--in -> 内存流


            for (String urlcrrent : URL_PATH) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            byte[] bytes = downByteArray(urlcrrent);
                            // 后缀
                             String stuffix =   getDataType(urlcrrent);
                            // 前缀
                             long preffix = System.currentTimeMillis();

                            saveDask(bytes,preffix+stuffix);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }


    }

    private static String getDataType(String urlcrrent) {
         int i = urlcrrent.lastIndexOf(".");
         return urlcrrent.substring(i);
    }

    /**
     *  内存数据保存到磁盘
     * @param bytes
     * @param s
     */
    private static void saveDask(byte[] bytes, String path) {
        try(
                FileOutputStream fileOutputStream = new FileOutputStream(path);
                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        )
         {
         // 写到磁盘
             bos.write(bytes);
             bos.flush();
         }catch(Exception e){
            System.out.println("写入失败");
            e.printStackTrace();
        }
    }

    /**
     * 下载网络资源到内存流
     *
     * @param urlcrrent 网络资源定位
     * @throws Exception 异常
     */
    private static byte[] downByteArray(String urlcrrent) throws Exception {
        URL url = new URL(urlcrrent);
        try (
                // 读
                InputStream inputStream = url.openStream();
                //装饰
                BufferedInputStream bis = new BufferedInputStream(inputStream);
                // --写到内存流中 in ->byteOut
                ByteArrayOutputStream baos = new ByteArrayOutputStream();

        ) {
            // 把 in -> 写到内存流
            byte[] data = new byte[1024];
            int len = 0;
            while ((len = bis.read(data)) != -1){
                baos.write(data, 0, len);
            }
            baos.flush();
            // 返回一个 byte数组
            return baos.toByteArray();

            // 把内存流写到
        } catch (Exception e) {
            System.out.println("写入内存失败");
            e.printStackTrace();
        }
        return  null;

    }


}
